package com.lianzhihui.romeex.ui.wallet;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.WalletMainBean;
import com.lianzhihui.romeex.bean.ZZConfigBean;
import com.lianzhihui.romeex.mvp.presenter.ZZConfigPresenter;
import com.lianzhihui.romeex.mvp.view.ZZConfigView;
import com.lianzhihui.romeex.ui.base.BaseFragment;
import com.lianzhihui.romeex.utils.ImagesUtil;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:跨链收款 界面
 * Author: RAMON
 * CreateDate: 11/13/20 5:32 PM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class ShouqianFragment extends BaseFragment implements ZZConfigView {
    private static final String ARG_PARAM1 = "param1";
    private WalletMainBean.CoinBean coin;
    @BindView(R.id.bt_save)
    Button bt_save;
    @BindView(R.id.tv_type)
    TextView tv_type;
    @BindView(R.id.iv_qrcode)
    ImageView iv_qrcode;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.tv_cross_warnning)
    TextView tv_cross_warnning;
    @BindView(R.id.ll_qr_range)
    LinearLayout ll_qr_range;
    ZZConfigBean bean;
    ZZConfigPresenter presenter;


    public static ShouqianFragment newInstance(WalletMainBean.CoinBean coin) {
        ShouqianFragment fragment = new ShouqianFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, coin);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            coin = (WalletMainBean.CoinBean) getArguments().get(ARG_PARAM1);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shouqian_layout;
    }

    @Override
    protected void initView() {
        presenter = new ZZConfigPresenter(activity, this);
        showLoading("");
        presenter.getConfig(coin.getCoinName(), coin.getCoinNameCobo(), 2);
    }

    /**
     * 生成二维码
     */
    private void madeQR() {

        String qrStr = bean.getWalletAddressCross();
        if (TextUtils.isEmpty(qrStr)) {
            toast("无数据");
        } else {
//            double sum = 0D;
//            String headQrStr = QRUtils.QR_CODE_WALLET_RO_FLAG + QRUtils.QR_CROSS + sum
//                    + "_" + qrStr + "_" + coin.getCoinNameAlias();
            ImagesUtil.createQrCodeImg(qrStr, iv_qrcode, 220);
            tv_address.setText(qrStr);
        }
    }

    @Override
    public void onCoinSuccess(ZZConfigBean bean) {
        dismissLoading();
        if (isAdded()) {
            //生成二维码
            this.bean = bean;
            madeQR();
            setWarning();
        }

    }

    private void setWarning() {
        tv_cross_warnning.setText(getString(R.string.cross_warning_p1) + coin.getCoinNameAlias() + getString(R.string.cross_warning_p2));
    }

    @Override
    public void onCoinFail(Object obj) {
        toast("无数据");
    }

    @Override
    public void setFailure() {
        toast("无数据");
    }

    /**
     * 保存二维码
     */
    private void save() {
        if (NoDoubleClickUtils.isDoubleClick()) {
            String[] PERMISSIONS = {
                    "android.permission.READ_EXTERNAL_STORAGE",
                    "android.permission.WRITE_EXTERNAL_STORAGE"};
            //检测是否有写的权限
            int permission = ContextCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS, 1);
            } else {
                if (bean != null) {
                    ImagesUtil.saveQrCode(activity, ll_qr_range);
                }
            }
        }
    }

    @OnClick({R.id.bt_save, R.id.tv_address})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_save:
                //保存二维码
                save();
                break;
            case R.id.tv_address:
                if (bean == null || TextUtils.isEmpty(bean.getWalletAddressCross())) {
                    copyString(" ");
                } else {
                    copyString(bean.getWalletAddressCross());
                }

                break;
            default:
                break;
        }
    }

}
